package org.zb.shiro.controller;


import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.zb.shiro.model.Role;
import org.zb.shiro.service.RoleService;
import org.zb.web.response.AjaxResult;

import com.github.pagehelper.Page;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@GetMapping("/add")
	public String add(Role role){
		return "role/add";
	}
	
	@PostMapping("/add")
	@ResponseBody()
	public AjaxResult add(@Valid Role role, BindingResult result) {
		AjaxResult rst = new AjaxResult();
		if(result.hasErrors()) {
			rst.setCode("error");
			rst.setValidatedResult(result);
			return rst;
		} 
		
		if(roleService.saveRole(role) > 0) {
			
			rst.setCode("success");
			rst.setMessage("添加成功");
		} else {
			
			rst.setCode("error");
			rst.setMessage("服务器错误");
		}
		
		return rst;
	}
	
	@GetMapping("/edit/{id}")
	public String edit(ModelMap map, @PathVariable(name="id") Long id){
		Role role = roleService.findById(id);
		map.addAttribute("role",role);
		return "role/edit";
	}
	
	@PostMapping("/edit/{id}")
	@ResponseBody()
	public AjaxResult edit(@PathVariable(name="id") Long id,@Valid Role role, BindingResult result) {
		AjaxResult rst = new AjaxResult();
		if(result.hasErrors()) {
			rst.setCode("error");
			rst.setValidatedResult(result);
			return rst;
		} 
		if (role != null) {
			role.setId(id);
			if(roleService.updateRole(role) > 0) {
				
				rst.setCode("success");
				rst.setMessage("更新成功");
			} else {
				
				rst.setCode("error");
				rst.setMessage("服务器错误");
			}
		} else {
			rst.setCode("error");
			rst.setMessage("用户不存在");
		}
		return rst;
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public int delete(@RequestParam Long id)
	{
		Role role = roleService.findById(id);
		return roleService.deleteRole(role);
	}
	
	@PostMapping("/patch-delete")
	@ResponseBody
	public int patchDelete(@RequestParam(name="ids[]") Long[] ids)
	{
		if(ids !=null) {
			return roleService.patchDeleteByIds(ids);
		}
		return 0;
	}
	
	@GetMapping("/list")
	public String list(ModelMap model,@RequestParam Map<String, Object> params){
		Page<Role> roles = (Page<Role>) roleService.searchAll(params);
		model.addAttribute("params",params);
		model.addAttribute("roles",roles);
		model.addAttribute("totalRecords",roles.getTotal());
		model.addAttribute("totalPages",roles.getPages());
		model.addAttribute("pageNum",roles.getPageNum());
		
		return "role/list";
	}
}
